<?php

namespace App\Http\Controllers\Admin;

use App\Constant\Constant;
use App\Models\Hotel\Hotel;
use App\Models\Product;
use App\Requests\Product\StoreRequest;
use Illuminate\Http\Request;
use App\Exceptions\ExceptionSms;
use Carbon\Carbon;
use App\Helpers\MoneyHelper;

/**
 * 套餐管理
 * Class ProductController
 * @package App\Http\Controllers\Admin
 */
class ProductController extends CommonController
{
    protected $model = Product::class;
    public function index($list_type)
    {
        $search_forms = Product::getForms(['name', 'type']);
        $search_forms['type']['value'] = $list_type;

        $cols = [[
            ['field' => 'entity_name', 'title' => '酒店'],
            ['field' => 'name', 'title' => '名称'],
            ['field' => 'thumbnail_format', 'title' => '封面', 'escape' => false],
            ['field' => 'price', 'title' => '价格'],
            ['field' => 'sort', 'title' => '排序', 'escape' => false],
            ['field' => 'is_enable_format', 'title' => '状态', 'escape' => false],
            ['field' => 'action', 'title' => '操作', 'escape' => false],
        ]];

        $permission_code = "product-{$list_type}-query";

        $uri = '/admin/product/list?code=' . $permission_code . '&type=' . $list_type;

        $top_btns = [
            ['code' => "product-{$list_type}-add", 'params' => ['type' => $list_type]]
        ];

        return view('components.list', compact('search_forms', 'cols', 'permission_code', 'uri', 'top_btns'));
    }

    public function dataList()
    {
        $list = Product::bgBaseQuery($this->input())->paginate($this->limit);
        /** @var Product $item */
        foreach ($list as $item) {
            if ($item->isHotelProduct()) {
                $item->entity_name = $item->hotel->name;
            }
            $item->thumbnail_format = $item->formatImage($item->thumbnail);
            $item->is_enable_format = $item->formatIsShelf($item->is_enable);
            $item->action = $item->setRowBtns([
                "product-{$item->type}-edit",
                "product-{$item->type}-delete",
            ], [
                "product-{$item->type}-enable" => [
                    ['is_enable' => 0]
                ],
                "product-{$item->type}-disable" => [
                    ['is_enable' => 1]
                ]
            ]);
        }
        return ExceptionSms::homeList($list->items(), $list->total());
    }

    public function editAdd()
    {
        $id = $this->input('id') ?? 0;
        $type = $this->input('type');
        $data = $id ? Product::getOneById($id) : null;
        $type = $data ? $data->type : $type;
        $save_forms = Product::getForms(['entity_id', 'name', 'thumbnail', 'price', 'sort', 'type'], 'save');
        $save_forms['type']['value'] = $type;
        if ($type == Constant::ProductType_Hotel) {
            $hotels = Hotel::select(['id as value', 'name as label'])->get();
            $save_forms['entity_id']['options'] = $hotels;
            $save_forms['thumbnail']['value'] = $data->thumbnail;
        }
        $permission_code = $id ? "product-{$type}-edit-save" : "product-{$type}-add-save";
        return view('components.save_form', compact('data', 'save_forms', 'permission_code'));
    }

    public function dataSave(StoreRequest $request)
    {
        $id = $request->get('id') ?? "";
        if ($id) $data = Product::getOneById($id);
        else $data = new Product();
        $data->entity_id = $request->get("entity_id");
        $data->type      = $request->get("type");
        $data->name      = $request->get("name");
        $data->thumbnail = $request->get("thumbnail");
        $data->price     = $request->get("price");
        $data->sort      = $request->get('sort');
        $data->save();
        return ExceptionSms::returnSuccess();
    }
}
